﻿Public Class frmLeaveTypeDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim sqlManager As New SQLManager
    Dim payrollManager As New PayrollManager

    Private Sub frmLeaveTypeDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        validator.ResetControls(grpLeaveType)
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                Dim leavetypeList As List(Of LeaveType) = payrollManager.getItemLeaveType(Clipboard.GetData(DataFormats.Text))
                For Each leavetype As LeaveType In leavetypeList
                    txtLeaveType.Text = leavetype.type
                    txtDescription.Text = leavetype.description
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtLeaveType, "Leave Type") Then Return
        If validator.IsEmptyString(txtDescription, "Description") Then Return
        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                sqlManager.executeQuery("INSERT INTO ref_leave_type (type, description) VALUE ('" + txtLeaveType.Text + "','" + txtDescription.Text + "')")
                MsgBox("Successfully saved.")
            Else
                sqlManager.executeQuery("UPDATE ref_leave_type SET type = '" + txtLeaveType.Text + "',description = '" + txtDescription.Text + "' WHERE id=" + Clipboard.GetData(DataFormats.Text) + "")
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpLeaveType)
        Else
            Me.Close()
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpLeaveType)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
End Class